package com.telewave.stompdemo.controller;

import com.telewave.stompdemo.domain.Greeting;
import com.telewave.stompdemo.domain.HelloMessage;
import com.telewave.stompdemo.utils.JsonUtil;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author looveh
 * @since 2023/10/17 14:24:55
 **/
@Controller
public class GreetingController {

    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/hello")
    // @SendTo("/topic/greetings")
    public void greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        Greeting greeting = new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
        simpMessagingTemplate.convertAndSend("/topic/greetings", JsonUtil.parseObjToJson(greeting));
        // return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }
    @MessageMapping("/hello1")
    // @SendTo("/topic/greetings")
    public void greeting1(HelloMessage message) throws Exception {
        // Thread.sleep(1000); // simulated delay
        Greeting greeting = new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
        simpMessagingTemplate.convertAndSend("/exchange/mcExchange/a.b.c.d", JsonUtil.parseObjToJson(greeting));
    }
}
